/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao.server;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import shared.Card;
import shared.PlayerInfo;

/**
 *
 * @author latata_2
 */
public class Player {

    private int points = 0;
    private String playerName;
    private HashSet<Card> cards;
    private PlayerConnection playerConnection = null;
    private int turnsToWait = 0;
    private int demandCardType = 0;

    /*public Player(int playerId, String playerName) {
    this.playerId = playerId;
    this.playerName = playerName;
    }*/
    public Player(String login, PlayerConnection playerConnection) {
        this.playerName = login;
        this.playerConnection = playerConnection;
        this.cards = new HashSet<Card>();
    }
    
    public void updatePoints(int points) {
       this.points += points;
    }

    public void setTurnsToWait(int turnsToWait) {
        this.turnsToWait = turnsToWait;
    }

    public int getTurnsToWait() {
        return turnsToWait;
    }

    public String getPlayerName() {
        return playerName;
    }
    
    public int getPoints() {
        return points;
    }

    /**
     * Daje kartę graczowi
     * @param card 
     */
    public void giveCard(Card card) {
        cards.add(card);
    }

    public HashSet<Card> getCards() {
        return cards;
    }

    public PlayerInfo getPlayerInfo() {
        return new PlayerInfo(playerName, cards.size(), points, turnsToWait, demandCardType);
    }

    public PlayerConnection getPlayerConnection() {
        return playerConnection;
    }

    public void setPlayerConnection(PlayerConnection playerConnection) {
        this.playerConnection = playerConnection;
    }

    public void setDemandCardType(int demandCardType) {
        this.demandCardType = demandCardType;
    }

    public boolean hasCards(ArrayList<Card> cards) {
        ///TODO
        HashSet<Card> tempCards = new HashSet<Card>();
        for (Card thrownCard : cards) {
            boolean found = false;
            for (Card playersCard : this.cards) {
                if (!found && !tempCards.contains(playersCard)
                        && playersCard.getType() == thrownCard.getType()
                        && playersCard.getSuit() == thrownCard.getSuit()) {
                    tempCards.add(playersCard);
                    found = true;
                }
            }
            if (!found) {
                return false;
            }
        }
        return true;
    }

    public void removeCard(Card card) {
        boolean removed = false;
        Iterator<Card> cardsIterator = cards.iterator();
        while(cardsIterator.hasNext() && !removed) {
            Card tempCard = cardsIterator.next();
            if(card.getType() == tempCard.getType() && card.getSuit() == tempCard.getSuit()) {
                cards.remove(tempCard);
                removed = true;
            }
        }
    }

    public void decreaseTurnsToWait() {
        this.turnsToWait--;
    }

    boolean hasCardType(int demandCardType) {
        for(Card card : cards) {
            if(card.getType() == demandCardType || card.getType() == Card.JOKER) {
                return true;
            }
        }
        return false;
    }
}
